import time

from HTstructure import *
import threading

# 加载共享库
lib = cdll.LoadLibrary('../toPythonSo/libtoPythonSo.so')

# 设置入参
# custid = b"0050011047"
# password = b"123456"
config_path = b"/home/drogon/config.ini"

# 初始化python变量
lib.SingleOrderInsert.argtypes = [c_void_p, POINTER(ReqSingleOrderInsertField)]
lib.SingleOrderInsert.restype = None

lib.trade_new.argtypes = [c_char_p]
lib.trade_new.restype = c_void_p

lib.trade_start.argtypes = [c_void_p]
lib.trade_start.restype = None

lib.get_pOrderField.argtypes = [c_void_p]
lib.get_pOrderField.restype = OrderField

lib.UserLogion.argtypes = [c_void_p, POINTER(ReqUserLoginField)]
lib.UserLogion.restype = None
# 跑api登录+客户登录
# 把api实例保持在python本地 tad
# 初始化客户信息结构体
cust_info = ReqUserLoginField(cust_type=b'0', cert_no=b"0050011047", password=b"123456")
# 无限循环做api初始化和客户登录，登录之前无法委托，所以登录流程做同步接口

tad = lib.trade_new(config_path)
lib.trade_start(tad)
time.sleep(10)
# 初始化委托结构体
order_instance = OrderType(exchangeId=b'1', orderRef=1, securityId=b'600837', direction=b'0', limitPrice=11.2,
                           totalOriginalVolume=100, orderType=b'0')
req_instance = ReqSingleOrderInsertField(order=order_instance, batchId=1)
# 跑api 普通委托
lib.SingleOrderInsert(tad, byref(req_instance))
# 等回调
time.sleep(10)
# 拿到api实例里面的回调后的委托数据
res_ptr = lib.get_pOrderField(tad)
print("推送后结构体")
print(f"sesId:{res_ptr.sessionId}")
print(f"ordRef:{res_ptr.orderRef}")
print(f"ordId:{res_ptr.orderId}")
print("secId:" + res_ptr.securityId.decode("utf-8"))
print(f"ordSts:{res_ptr.orderStatus}")
print(f"entrust:{res_ptr.totalOriginalVolume}")
print(f"traded:{res_ptr.tradedVolume}")
print(f"cancelled:{res_ptr.cancelVolume}")
